package com.ste.client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class LoginFrame extends JFrame {

	private JPanel contentPane;
	private JTextField ipField;
	private JTextField portField;
	private JTextField clientNameField;
	private View view;

	public LoginFrame(final View view) {
		this.view = view;
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setBounds(100, 100, 307, 179);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblIpAddress = new JLabel("Ip address");
		lblIpAddress.setBounds(10, 40, 61, 20);
		contentPane.add(lblIpAddress);

		ipField = new JTextField();
		ipField.setText("127.0.0.1");
		ipField.setBounds(97, 40, 86, 20);
		contentPane.add(ipField);
		ipField.setColumns(10);

		JLabel lblPort = new JLabel("Port");
		lblPort.setBounds(10, 71, 46, 14);
		contentPane.add(lblPort);

		portField = new JTextField();
		portField.setText("4444");
		portField.setBounds(97, 68, 86, 20);
		contentPane.add(portField);
		portField.setColumns(10);

		JLabel lblClientId = new JLabel("Client ID");
		lblClientId.setBounds(10, 15, 46, 14);
		contentPane.add(lblClientId);

		clientNameField = new JTextField();
		clientNameField.setText("Client");
		clientNameField.setBounds(97, 9, 86, 20);
		contentPane.add(clientNameField);
		clientNameField.setColumns(10);

		JButton btnLogon = new JButton("Logon");
		btnLogon.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (view.getCommandsHandler().getClientName() == null) {
					Connector connector = new Connector(view);
					view.setConnector(connector);
					connector.connect(ipField.getText(), portField.getText());
					view.getCommandsHandler().registerClient(clientNameField.getText());
				}else{
					setVisible(false);
				}
			}
		});
		btnLogon.setBounds(94, 107, 89, 23);
		contentPane.add(btnLogon);
	}

}
